package com.example.myprojectoffeixue;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.HashMap;
/**
 * Created by ASUS on 2017/2/14.
 */
public class adapter_Choicesb extends BaseAdapter {
    private ArrayList<SheBeiClass> d;
    private  MyApplication m;
    private Context context;
    private KtGroupActivity Kt;
    private LayoutInflater layoutInflater;
    private HashMap<String, String> kt_map;
    public HashMap<String, Extra> kt2_map;
    public ArrayList<String> kt_flag;
    public boolean flag = false;
    public adapter_Choicesb(Context context, ArrayList<String> kt_flag) {
        super();
        this.context = context;
        ChoiceSbActivity sd= (ChoiceSbActivity) context;
        m= (MyApplication) sd.getApplication();
        this.d = m.Nodevice_house.devices;
        this.kt_flag = kt_flag;
        this.layoutInflater = LayoutInflater.from(context);
    }
    @Override
    public int getCount() {
        return d.size();
    }

    @Override
    public Object getItem(int position) {
        return d.get(position);
    }
    @Override
    public long getItemId(int position) {
        return position;
    }
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder h = null;
        if (convertView == null) {
            h = new ViewHolder();
            convertView = layoutInflater.inflate(R.layout.item_device, null);
            h.flag_kt = (ImageView) convertView.findViewById(R.id.iv_addcjsb);
            h.kt = (ImageView) convertView.findViewById(R.id.image_KT);
            h.name = (TextView) convertView.findViewById(R.id.name_item);
            h.all_Item = (LinearLayout) convertView.findViewById(R.id.All_item);
            convertView.setTag(h);
        } else {
            h = (ViewHolder) convertView.getTag();
        }
        if (d.get(position).deviceId != null) {
            if ("0100".equals(d.get(position).deviceId)) {
                //墙面三路开关
                h.kt.setImageResource(R.drawable.kai);


            } else if ("010B".equals(d.get(position).deviceId)||"010A".equals(d.get(position).deviceId)) {
                //智能门锁
                h.kt.setImageResource(R.drawable.ms);

            } else if ("010D".equals(d.get(position).deviceId)) {
//门磁
                h.kt.setImageResource(R.drawable.mc);

            } else if ("010E".equals(d.get(position).deviceId)) {
//红外
                h.kt.setImageResource(R.drawable.hw2);
            } else if ("010C".equals(d.get(position).deviceId)) {
//取电
                h.kt.setImageResource(R.drawable.qudiana);
            } else if ("0002".equals(d.get(position).deviceId)) {
                h.kt.setImageResource(R.drawable.cl2);

            } else if ("0009".equals(d.get(position).deviceId)) {
                h.kt.setImageResource(R.drawable.chazuo);
            } else if ("0020".equals(d.get(position).deviceId)) {
                h.kt.setImageResource(R.drawable.hwzfq);
            }else if ("010F".equals(d.get(position).deviceId)) {
                h.kt.setImageResource(R.drawable.zykt);
            }

        }
        h.name.setText(d.get(position).deviceName);
        if (flag) {
            h.flag_kt.setVisibility(View.VISIBLE);
        }

        if ("1".equals(kt_flag.get(position))) {
            h.flag_kt.setImageResource(R.drawable.button_kuang_yixuan);
        } else if ("0".equals(kt_flag.get(position))) {
            h.flag_kt.setImageResource(R.drawable.button_kuang_weixuan);
        }
        final ViewHolder finalH = h;
        Log.d("size3",kt_flag.size()+"");
        h.all_Item.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("1".equals(kt_flag.get(position))) {
                    kt_flag.set(position, "0");
                    finalH.flag_kt.setImageResource(R.drawable.button_kuang_weixuan);
                } else if ("0".equals(kt_flag.get(position))) {

                    kt_flag.set(position, "1");
                    finalH.flag_kt.setImageResource(R.drawable.button_kuang_yixuan);
                }

            }
        });
        return convertView;
    }

    class ViewHolder {

        TextView name;
        TextView current_Wd;
        TextView Kt_Wd;
        LinearLayout all_Item;
        ImageView flag_kt;
        ImageView kt;

    }


}
